<template>
  <a-select v-model:value="trainCode" show-search
            :filter-option="filterStationCodeOption" placeholder="请选择车站"
            @change="onChange" :style="'width:' + _width">

    <a-select-option v-for="item in stations" :key="item.name"
                     :value="item.name" :label="item.name + item.namePinyin + item.namePy">
      {{item.name}} | {{item.namePinyin}}
    </a-select-option>

  </a-select>
</template>

<script>
import {defineComponent, onMounted, ref, watch} from 'vue';
import axios from "axios";
import {notification} from "ant-design-vue";
export default defineComponent({
  name: "train-select-view",
  props:["modelValue"],
  emits:['update:modelValue','change'],
  setup(props, {emit}) {
    const trainCode = ref();
    /**
     * 获取所有火车车次,用于车位下拉框
     */

    //利用watch，动态获取父组件的值，如果放在onMounted或其他方法只会执行一次
    watch(() => props.modelValue, ()=>{
      console.log("props.modelValue",props.modelValue)
      trainCode.value = props.modelValue;
    },{immediate: true})

    const stations = ref([]);

    const queryStation = () =>{
      axios.get("/business/station/query-all").then((response) => {
        const data = response.data;
        if (data.success) {
          stations.value = data.content
        } else {
          notification.error({description: data.message});
        }
      })
    };
    /**
     * 车位下拉框筛选
     */
    const filterStationCodeOption = (input, option) => {
      console.log(input,option);
      return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }

    /**
     * 将当前组件的值响应给父组件
     * @param value
     */
    const onChange = (value) => {
      emit('update:modelValue', value);
    };

    onMounted(() => {
      queryStation()
    })
    return {
      stations,
      queryStation,
      filterStationCodeOption,
      trainCode,
      onChange,
    };
  },
});
</script>

<style>

</style>